data = load('/Users/manojnair/Downloads/rms3HourFit_15mKnot_20100101_20101231_at_Ipm');
%data = load('/Users/manojnair/Downloads/rmsFit_15mKnot_20100101_20101231_at_Ipm');

load /Users/manojnair/projects/trackline_error/Ap_1950_2012 Ap fday_Ap

% find common points
[ia,ib,c] = intersect(fday_Ap, data(:,1));
fdayap = fday_Ap(ib);
apdata = Ap(ib);

%% plot the correlation
plot(apdata,data(:,2),'r.','MarkerSize',20)
set(gca,'FontSize',20);
hold on;
%
% plot the tsunami point
plot(apdata(461),data(461,2),'bo','MarkerSize',20,'LineWidth',2);
xlabel('ap index');
ylabel('RMS of Z residual ');
axis([-5,200 -0.05 0.5]);

% plot April 2 2014 data

load /Users/manojnair/projects/tsunami/cbi_2014_Apr_2_resid residual;

residual(1:51) = 0;
residual(end-51:end) = 0;

for i = 1:8,
   
ipmresid(i) = rms(residual((i-1)*3600+1:(i*3*3600)));

end;

fdayipm = datenum(2014,04,02,0:3:21,0,0);
% find common points
apdata = [2  0  0  3  5  5  5  5] % from WDC
plot(apdata,ipmresid,'r.','MarkerSize',20);

plot(apdata(3),ipmresid(3),'bo','MarkerSize',20,'LineWidth',2);



% plot(fday_Ap, 0.2+ Ap/400,'r')
% hold on
% plot(data(:,1),data(:,2),'r')
% axis([min(data(:,1)), max(data(:,1)) -inf inf])
% legend('0.2 + Ap/400','Resid')
% datetick('x','keeplimits');

%%

load  /Users/manojnair/projects/tsunami/cbi_2011_jan_march_resid residual
% %padd zeros to start and end discraded data
% 
% discard_start_index = floor(86400*(1-0.9994));
% discard_end_index = floor(86400*0.9994);
% residual = [zeros([51,88]) residual' ...
%     zeros([51,88])];

residual = padarray(residual, [51,0]);

% remove noisy data on 2011-01-24
residual(:,24) = NaN;
residual(:,62) = NaN;
fdaycbi = datenum(2011,1,1,0:3:2109,0,0)';
for i = 1:8,
    for j = 1:88,
cbi_resid(i,j) = rms(residual((i-1)*3600+1:(i*3*3600),j));
    end;
end;

cbiresid = cbi_resid(:);

load /Users/manojnair/projects/trackline_error/Ap_1950_2012 Ap fday_Ap

%% find common points
[ia,ib,c] = intersect(fday_Ap, fdaycbi);
fdayap = fday_Ap(ib);
apdata = Ap(ib);

plot(apdata, cbiresid,'r.','MarkerSize',20);
set(gca,'FontSize',20);
hold on;

L = cbiresid > 0.25;
plot(apdata(L),cbiresid(L),'bo','MarkerSize',20,'LineWidth',2);


xlabel('ap index');
ylabel('RMS of Z residual ');
axis([-5,100 -0.05 0.5]);
